﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;

namespace _4SQ
{
    public partial class MainPage : PhoneApplicationPage
    {
        private IsolatedStorageSettings appSettings; // Store OAuth

        // OAuth Settings
        string CLIENT_ID = "NTPDXNENSAJSHR0SIGVMRD5VWTKV1Y22ARQQZV4CNDZXUTRC";
        string CALLBACK_URI = "http://lance.compulsivetech.biz";
        string response; // Stores OAuth Code

        private string OAuthSavedCode = "";
       // private string wasIAuth = "";

       // private const string wasAuth = "";
        private const string theAuthCode = "";

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            

            string address =
            "https://foursquare.com/oauth2/authenticate" +
            "?client_id=" + CLIENT_ID +
            "&response_type=token&display=touch" +
            "&redirect_uri=" + CALLBACK_URI;

            webBrowser1.Navigated += new EventHandler<NavigationEventArgs>(webBrowser1_Navigated);
            webBrowser1.Navigate(new Uri(address, UriKind.Absolute));

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            appSettings = IsolatedStorageSettings.ApplicationSettings; // Get settings from Store
            // Check if OAuth was Set/Saved
            string wasdone = (string)appSettings[theAuthCode];

            if (appSettings.Contains(theAuthCode))
            {
                if (wasdone == "")
                {
                    MessageBox.Show("Welcome to 4Square!\n\nSince this is your first time using this App," +
                        "we need you to allow this App by logging in and telling Foursquare it's ok.",
                        "4Square - Login Needed", MessageBoxButton.OK);
                }
                else
                {
                    // Put Auth Code for Main App
                    OAuthSavedCode = (string)appSettings[OAuthSavedCode];

                    // Load Main App
                    NavigationService.Navigate(new Uri("/MainApp.xaml?OAuthCode=" + OAuthSavedCode, UriKind.Relative));
    
                }
                
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            appSettings.Add(theAuthCode, OAuthSavedCode); // Save we've been Auth'd
        }

        void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            response = e.Uri.ToString();
            System.Diagnostics.Debug.WriteLine(e.Uri.ToString());
            if (response.LastIndexOf("access_token=") != -1)
            {
                string token = response.Substring(response.LastIndexOf("#access_token=") + "#access_token=".Length);
                System.Diagnostics.Debug.WriteLine("TOKEN: " + token);

               OAuthSavedCode = token; // Save our Token
              // wasIAuth = "yes"; // Save that we've been Auth'd

            }
        }

    }
}